/**
 * 消费明细页面类
 * 业务层级：表示层
 * 功能说明：提供会员消费明细的查看界面，支持按会员、等级、时间等条件筛选和展示消费记录
 * 架构关系：遵循四层架构设计，作为MM模块的第三个堆栈窗口，展示会员消费明细信息
 */
#ifndef CONSUMPTIONDETAILPAGE_H
#define CONSUMPTIONDETAILPAGE_H

#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QDateEdit>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QHeaderView>
#include <QScrollArea>
#include <QFrame>
#include <QGroupBox>
#include <QSplitter>
#include <QProgressBar>
#include <QTimer>
#include <QDateTime>
#include <QMap>
#include <QVector>
#include <QVariant>
#include <QMessageBox>

// 前置声明
class Database;
class ConsumptionService;

class ConsumptionDetailPage : public QWidget
{
    Q_OBJECT

public:
    explicit ConsumptionDetailPage(QWidget *parent = nullptr);
    ~ConsumptionDetailPage();

private slots:
    // 筛选和查询槽函数
    void onMemberFilterChanged();
    void onLevelFilterChanged();
    void onStatusFilterChanged();
    void onPaymentMethodChanged();
    void onSearchButtonClicked();
    void onResetFiltersClicked();
    void onRefreshDataClicked();
    
    // 排序槽函数
    void onSortByTimeClicked();
    void onSortByAmountClicked();
    void onTableHeaderClicked(int logicalIndex);
    
    // 详情查看槽函数
    void onViewOrderDetailsClicked();
    void onTableItemDoubleClicked(QTableWidgetItem *item);
    
    // 导出功能槽函数
    void onExportDataClicked();
    void onPrintReportClicked();
    
    // 统计信息更新槽函数
    void onUpdateStatistics();

private:
    // UI初始化函数
    void initUI();
    void createFilterPanel();
    void createDataTable();
    void createStatisticsPanel();
    void createActionButtons();
    
    // 数据处理函数
    void loadConsumptionData();
    void loadMemberList();
    void loadLevelList();
    void loadPaymentMethods();
    void applyFilters();
    void updateTableData(const QVector<QMap<QString, QVariant>> &data);
    void updateStatisticsDisplay(const QVector<QMap<QString, QVariant>> &data);
    
    // 样式设置函数
    void setupTableStyle();
    void setupFilterPanelStyle();
    void setupStatisticsPanelStyle();
    void setupButtonStyles();
    void applyGreenTheme();
    
    // 工具函数
    QString formatAmount(double amount);
    QString formatDateTime(const QDateTime &dateTime);
    QString getStatusDisplayText(int status);
    QString getStatusColorCode(int status);
    void showOrderDetailsDialog(int orderId);
    void exportToExcel(const QVector<QMap<QString, QVariant>> &data);
    void printReport(const QVector<QMap<QString, QVariant>> &data);
    void showStyledMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text);
    
    // 验证函数
    
    // 成员变量：数据库和服务
    Database *m_db;
    ConsumptionService *m_consumptionService;
    
    // 成员变量：主布局
    QVBoxLayout *m_mainLayout;
    QSplitter *m_mainSplitter;
    
    // 筛选控件
    QGroupBox *m_filterGroup;
    QComboBox *m_memberCombo;
    QComboBox *m_levelCombo;
    QComboBox *m_statusCombo;
    QComboBox *m_paymentMethodCombo;
    QPushButton *m_searchBtn;
    QPushButton *m_resetBtn;
    QPushButton *m_refreshBtn;
    
    // 成员变量：数据表格
    QTableWidget *m_dataTable;
    QVector<QMap<QString, QVariant>> m_currentData;
    QVector<QMap<QString, QVariant>> m_originalData;
    
    // 成员变量：统计面板
    QGroupBox *m_statisticsGroup;
    QGridLayout *m_statisticsLayout;
    QLabel *m_totalOrdersLabel;
    QLabel *m_totalAmountLabel;
    QLabel *m_totalDiscountLabel;
    QLabel *m_avgOrderAmountLabel;
    QLabel *m_memberCountLabel;
    QLabel *m_dateRangeLabel;
    QProgressBar *m_discountRateBar;
    
    // 成员变量：操作按钮
    QHBoxLayout *m_buttonLayout;
    QPushButton *m_viewDetailsBtn;
    QPushButton *m_sortByTimeBtn;
    QPushButton *m_sortByAmountBtn;
    QPushButton *m_exportBtn;
    QPushButton *m_printBtn;
    
    // 成员变量：排序状态
    bool m_timeSortAscending;
    bool m_amountSortAscending;
    int m_lastSortColumn;
    Qt::SortOrder m_lastSortOrder;
    
    // 成员变量：缓存数据
    QMap<int, QString> m_memberCache;
    QMap<int, QString> m_levelCache;
    QStringList m_paymentMethodCache;
    
    // 成员变量：定时器
    QTimer *m_refreshTimer;
    
    // 常量定义
    static const int COLUMN_ORDER_ID = 0;
    static const int COLUMN_MEMBER_NAME = 1;
    static const int COLUMN_MEMBER_LEVEL = 2;
    static const int COLUMN_ORDER_TIME = 3;
    static const int COLUMN_TOTAL_AMOUNT = 4;
    static const int COLUMN_DISCOUNT_AMOUNT = 5;
    static const int COLUMN_FINAL_AMOUNT = 6;
    static const int COLUMN_PAYMENT_METHOD = 7;
    static const int COLUMN_ORDER_STATUS = 8;
    static const int COLUMN_COUNT = 9;
};

#endif // CONSUMPTIONDETAILPAGE_H
